# *_*coding:utf-8 *_*
# @Author : Reggie
# @Time : 2022/1/12 19:57

import asyncio
import copy
import logging
from copy import deepcopy
from typing import List


if __name__ == '__main__':
    root_logger = logging.getLogger()
    stream_handle = logging.StreamHandler()
    formatter = logging.Formatter("%(asctime)s -- %(levelname)s --"
                                  " %(message)s (%(funcName)s in %(filename)s %(lineno)d)")
    stream_handle.setFormatter(formatter)
    root_logger.addHandler(stream_handle)
    root_logger.setLevel(logging.DEBUG)

    loop = asyncio.get_event_loop()

    rw = [1, 2, 3]
    ro = [1, 2, 3]
    deny = [1, 2, 3]


    class T:
        def __init__(self):
            self.client = {'ro': [1, 4], 'rw': [2, 5], 'deny': [3, 6]}

        async def client_add(self, rw: str = None, ro: str = None, deny: str = None):
            print("client_add", rw, ro, deny)
            return 0, "success"

        async def client_sub(self, rw: str = None, ro: str = None, deny: str = None):
            print("client_sub", rw, ro, deny)
            return 0, "success"

        async def client_del(self):
            err, msg = 0, "success"
            client_copy = copy.deepcopy(self.client)
            for key, value in client_copy.items():
                for v in value:
                    err, msg = await self.client_sub(**{key: v})
                    if err != 0:
                        return err, msg
                    if v in self.client[key]:
                        self.client[key].remove(v)
            return err, msg

        async def client_set(self, rw: List[str] = None, ro: List[str] = None, deny: List[str] = None):
            err, msg = 0, "success"

            async def handle(handle_key, set_array):
                if set_array:
                    add_array = set(set_array) - set(self.client[handle_key])
                    rm_array = set(self.client[handle_key]) - set(set_array)
                    logging.debug(f"handle_key: {handle_key} set_array: {set_array}")
                    logging.debug(f"will add_array {add_array}")
                    logging.debug(f"will rm_array {rm_array}")
                    logging.debug(f"result will be {set(set_array) & set(self.client[handle_key]) - rm_array}")
                    for add_value in add_array:
                        _code, _msg = await self.client_add(**{handle_key: add_value})
                        if _code != 0:
                            return _code, _msg
                        if add_value not in self.client[handle_key]:
                            self.client[handle_key].append(add_value)
                    for rm_value in rm_array:
                        _code, _msg = await self.client_sub(**{handle_key: rm_value})
                        if _code != 0:
                            return _code, _msg
                        if rm_value in self.client[handle_key]:
                            self.client[handle_key].remove(rm_value)
                return 0, "success"

            handle_dict = {
                "rw": rw,
                "ro": ro,
                "deny": deny,
            }
            for key, value in handle_dict.items():
                err, msg = await handle(key, value)
                if err != 0:
                    return err, msg
            return err, msg


    t = T()
    out = loop.run_until_complete(t.client_set(rw, ro, deny))
    print(out)
    print(t.client)
    out = loop.run_until_complete(t.client_del())
    print(out)
    print(t.client)
    print(t.client)
    print(t.client)
    loop.run_forever()
